package icbc.nav.control
{
	import com.greensock.TweenLite;
	import com.greensock.easing.Strong;
	
	import flash.display.DisplayObject;
	import flash.display.GradientType;
	import flash.display.Loader;
	import flash.display.LoaderInfo;
	import flash.display.SpreadMethod;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.IOErrorEvent;
	import flash.events.MouseEvent;
	import flash.filters.BevelFilter;
	import flash.filters.DropShadowFilter;
	import flash.filters.GlowFilter;
	import flash.geom.Matrix;
	import flash.net.URLRequest;
	import flash.text.TextField;
	import flash.text.TextFieldAutoSize;
	import flash.text.TextFormat;
	
	import icbc.app.AppGlobal;
	
	public class BotMenuItemControl extends Sprite
	{
		public static var EVENT_CLICK:String = "HELPBUTTON_EVENT_CLICK";
		public static const STATE_NORMAL:uint = 0;
		public static const STATE_CLICKED:uint = 1;
		private static const ICON_WIDTH:uint = 39;
		private var iconWidth:uint = ICON_WIDTH;
		private var gap:uint = 5;
		public var data:Object;
		private var controlWidth:uint;
		private var controlHeight:uint;
		private var rounded:uint = 20;
		public var state:uint = 0;
		private var borderColor:uint = 0xAAAAAA;
		private var baseColor:uint = 0xffffff;
		private var baseRedColor:uint = 0xC52027;
		
		private var main:Sprite = new Sprite();
		private var base:Sprite = new Sprite();
		private var over:Sprite = new Sprite();
		private var press:Sprite = new Sprite();
		private var icon:Sprite = new Sprite();
		private var clicked:Sprite = new Sprite();
		private var panel:Sprite;
		
		public function BotMenuItemControl(data:Object, vWidth:uint, vHeight:uint)
		{
			super();
			this.data = data;
			controlWidth = vWidth;
			controlHeight = vHeight;
			iconWidth = data.icon != null ? ICON_WIDTH : 0;
			addChild(main);
			main.addChild(base);
			main.addChild(over);
			main.addChild(clicked);
			main.addChild(icon);
			main.addChild(press);
			addEventListener(Event.ADDED_TO_STAGE, initStage);
		}
		
		private function initStage(evt:Event):void
		{
			removeEventListener(Event.ADDED_TO_STAGE, initStage);
			display();
		}
		
		private function display():void
		{
			buildBase();
			buildOver();
			buildClicked();
			buildIcon();
			buildPress();
			addListeners();
		}
		
		private function build(item:Sprite, bgColor:uint, iconColor:uint, textColor:uint):void
		{
			setRootBase(item, bgColor, iconColor, textColor);
		}
		
		private function setRootBase(item:Sprite, bgColor:uint, iconColor:uint, textColor:uint):void
		{
			var rounded:uint = 0;
			item.graphics.lineStyle(1, borderColor);
			item.graphics.beginFill(iconColor);
			item.graphics.drawRoundRect(0, 0, controlWidth, controlHeight, rounded, rounded);
			item.graphics.endFill();
			item.alpha = 0;
		}
		
		private function addListeners():void
		{
			press.addEventListener(MouseEvent.MOUSE_OVER, onRollOver);
			press.addEventListener(MouseEvent.MOUSE_OUT, onRollOut);
			press.addEventListener(MouseEvent.MOUSE_DOWN, onPress);
			press.addEventListener(MouseEvent.MOUSE_UP, onRelease);
		}
		
		private function removeListeners():void
		{
			press.removeEventListener(MouseEvent.MOUSE_OVER, onRollOver);
			press.removeEventListener(MouseEvent.MOUSE_OUT, onRollOut);
			press.removeEventListener(MouseEvent.MOUSE_DOWN, onPress);
			press.removeEventListener(MouseEvent.MOUSE_UP, onRelease);
		}
		
		private function onRollOver(evt:Event):void
		{
			var tween:TweenLite = new TweenLite(over,1,{ease:Strong.easeOut,alpha:1});
		}
		
		private function onRollOut(evt:Event):void
		{
			var tween:TweenLite = new TweenLite(over,2,{ease:Strong.easeOut,alpha:0});
			press.alpha = 0;
		}
		
		private function onPress(evt:Event):void
		{
			var tween:TweenLite = new TweenLite(press,0.5,{ease:Strong.easeOut,alpha:1});
		}
		
		private function onRelease(evt:Event):void
		{
			var tween:TweenLite = new TweenLite(press,1,{ease:Strong.easeOut,alpha:0});
			dispatchEvent(new Event(EVENT_CLICK));
			setClickedState();
		}
		
		private function buildBase():void
		{
			build(base, baseColor, baseColor, 0x000000);
			base.alpha = 1;
		}
		
		private function buildOver() : void
		{
			build(over, baseColor, baseRedColor, 0x000000);
		}
		
		private function buildClicked() : void
		{
			build(clicked, baseColor, baseRedColor, 0x000000);
		}
		
		private function buildPress() : void
		{
			build(press, 0x282928, 0x282928, 0xffffff);
		}
		
		private function buildIcon():void
		{
			if(data.icon == null || data.icon == "")
			{
				return;
			}
			var iconPath:String = AppGlobal.combinePath(data.icon);
			var loader:Loader = new Loader();
			loader.contentLoaderInfo.addEventListener(Event.COMPLETE, imageComplete);
			loader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR, onIOError);
			loader.load(new URLRequest(iconPath));
			icon.addChild(loader);
		}
		
		private function onIOError(evt:IOErrorEvent):void
		{
			trace("MenuButton.Image["+ data.icon +"].IOError: "+ evt.text);
		}		
		
		private function imageComplete(evt:Event):void
		{
			var image:DisplayObject = (evt.target as LoaderInfo).content;
			icon.x = controlWidth/2 - image.width/2;
			icon.y = controlHeight/2 - image.height/2;
		}
		
		public function dispose():void
		{
			removeListeners();
		}
		
		public function clearState():void
		{
			clicked.alpha = 0;
			state = STATE_NORMAL;
		}
		
		private function buildArrowSymbol(color:uint, sense:uint):Sprite
		{
			var aHeight:uint = 5;
			var aWidth:uint = 16;
			var sprite:Sprite = new Sprite();
			sprite.graphics.lineStyle(1, color);
			if(sense == 0)
			{
				sprite.graphics.lineTo(0,0);
				sprite.graphics.lineTo(aWidth/2,aHeight);
				sprite.graphics.lineTo(aWidth,0);
			} else {
				sprite.graphics.moveTo(0,aHeight);
				sprite.graphics.lineTo(aWidth/2,0);
				sprite.graphics.lineTo(aWidth,aHeight);
			}
			return sprite;
		}
		
		public function setClickedState():void
		{
			state = STATE_CLICKED;
			clicked.alpha = 1;
		}
	}
}